package handlers

import (
	"encoding/json"
	"game-master-sys-a/cache"
	"game-master-sys-a/models"
	"github.com/gin-gonic/gin"
	"time"
)

func Add(c *gin.Context) {
	name := c.PostForm("name")
	upload := c.PostForm("upload")
	stack := c.PostForm("stack")
	if name == "" || upload == "" || stack == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := models.App{
		Name:   name,
		Upload: upload,
		Stack:  stack,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "发表失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "发表成功",
	})
	return
}
func List(c *gin.Context) {
	var app []models.App
	redis_key := "list_app_"
	models.DB.Find(&app)
	jsonData, _ := json.Marshal(&app)
	cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)

	c.JSON(200, gin.H{
		"code": 200,
		"data": app,
		"msg":  "查询成功",
	})
	return

}
func Detail(c *gin.Context) {
	id := c.Query("id")
	var app []models.App
	redis_key := "list_app_"
	models.DB.Where("id=?", id).First(&app)
	jsonData, _ := json.Marshal(&app)
	cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)

	c.JSON(200, gin.H{
		"code": 200,
		"data": app,
		"msg":  "查询成功",
	})
	return

}
